using System;
using Microsoft.Practices.ServiceLocation;

namespace Caramel
{
	public static class ServiceLocatorExtensions
	{
		public static T TryGetInstance<T>(this IServiceLocator locator)
		{
			try
			{
				return locator.GetInstance<T>();
			}
			catch
			{
				return default(T);
			}
		}

		public static T TryGetInstance<T>(this IServiceLocator locator, string key)
		{
			try
			{
				return locator.GetInstance<T>(key);
			}
			catch (ActivationException)
			{
				return default(T);
			}
		}
	}
}
